package com.sevenyear.excelanalyze.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sevenyear.excelanalyze.common.result.Result;
import com.sevenyear.excelanalyze.entity.EvaporationRate;
import com.sevenyear.excelanalyze.entity.SurfaceWaterIrrigationRate;
import com.sevenyear.excelanalyze.entity.param.queryConditionParam.RateQueryCondition;
import com.sevenyear.excelanalyze.service.PartitionBaseInfoService;
import com.sevenyear.excelanalyze.service.SurfaceWaterIrrigationRateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 * 地表水灌溉利用系数 前端控制器
 * </p>
 *
 * @author sevenyear
 * @since 2021-08-20
 */
@RestController
@CrossOrigin
@Api(value = "地表水灌溉利用系数接口", tags = "地表水灌溉利用系数相关接口")
@RequestMapping("/surfaceWaterIrrigationRate")
public class SurfaceWaterIrrigationRateController {

    @Autowired
    PartitionBaseInfoService partitionBaseInfoService;
    @Autowired
    SurfaceWaterIrrigationRateService surfaceWaterIrrigationRateService;

    @ApiOperation(value = "Excel批量导入")
    @PostMapping("/insert/excel")
    public Result insertRainfallInfiltrationRateByExcel(MultipartFile file) {
        surfaceWaterIrrigationRateService.insertRainfallInfiltrationRateByExcel(file, surfaceWaterIrrigationRateService, partitionBaseInfoService);
        return Result.ok();
    }

    @ApiOperation(value = "条件带分页查询")
    @PostMapping("/query/{current}/{limit}")
    public Result queryPageCondition(@PathVariable long current, @PathVariable long limit,
                                     @RequestBody(required = false) RateQueryCondition rateQueryCondition) {
        //创建page对象
        Page<SurfaceWaterIrrigationRate> page = new Page<>(current, limit);
        surfaceWaterIrrigationRateService.queryPageByCondition(page, rateQueryCondition);
        long total = page.getTotal();
        List<SurfaceWaterIrrigationRate> records = page.getRecords();
        return Result.ok().data("total", total).data("list", records);
    }

    @ApiOperation(value = "手动添加单条记录")
    @PostMapping("/insert")
    public Result insert(@RequestBody(required = true) SurfaceWaterIrrigationRate surfaceWaterIrrigationRate) {
        boolean flag = surfaceWaterIrrigationRateService.saveOrUpdate(surfaceWaterIrrigationRate);
        if (flag) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }

    @ApiOperation(value = "根据主键id删除信息")
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable Long id) {
        boolean flag = surfaceWaterIrrigationRateService.removeById(id);
        if (flag) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }

    @ApiOperation(value = "查询所有信息，供excel导出使用")
    @GetMapping("/query/allList")
    public Result queryAllPartitionBaseInfo() {
        List<SurfaceWaterIrrigationRate> allList = surfaceWaterIrrigationRateService.list(null);
        return Result.ok().data("allList", allList);
    }

}

